package org.cem.security;

import javax.security.auth.callback.ConfirmationCallback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class LoginDialog extends Dialog {

	private static final long serialVersionUID = -6286257943883979741L;
	
	private Text user;
	private Text password;
	
	private ConfirmationCallback confirmationCallback;
	private TextOutputCallback messageCallback;
	private NameCallback nameCallback;
	private PasswordCallback passwordCallback;

	public LoginDialog(IShellProvider parentShell) {
		super(parentShell);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public LoginDialog(Shell shell) {
		super(shell);
		setShellStyle(SWT.SYSTEM_MODAL);
	}

	public void init(ConfirmationCallback confirmationCallback,
			TextOutputCallback messageCallback, NameCallback nameCallback,
			PasswordCallback passwordCallback) {
		this.confirmationCallback = confirmationCallback;
		this.messageCallback = messageCallback;
		this.nameCallback = nameCallback;
		this.passwordCallback = passwordCallback;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite content = (Composite) super.createDialogArea(parent);
		content.setLayout(new GridLayout(2, false));
		
		Label lblLogo = new Label(content, SWT.NONE);
		GridData logoGridData = new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1);
		logoGridData.horizontalSpan = 2;
		lblLogo.setLayoutData(logoGridData);
		lblLogo.setImage(ResourceManager.getPluginImage(CemSecurityPlugin.PLUGIN_ID, "icons/cem_logo.png"));
		
		Label lblBenutzername = new Label(content, SWT.NONE);
		lblBenutzername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblBenutzername.setText("Benutzer:");
		
		user = new Text(content, SWT.BORDER);
		user.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		user.forceFocus();
		
		Label lblPasswort = new Label(content, SWT.NONE);
		lblPasswort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPasswort.setText("Passwort:");
		
		password = new Text(content, SWT.BORDER | SWT.PASSWORD);
		password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		return content;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.get().OK_LABEL, true);
	}

	@Override
	protected void okPressed() {
		nameCallback.setName(user.getText());
		passwordCallback.setPassword(password.getTextChars());
//		confirmationCallback.setSelectedIndex(ConfirmationCallback.OK);
		super.okPressed();
	}

	@Override
	protected Point getInitialSize() {
		return new Point(350, 250);
	}
	
}
